﻿package ua.mihaylenko.canvas.gui.photoshop.canvas 
{
	import flash.geom.Rectangle;
	import ua.mihaylenko.canvas.controller.CanvasEvent;
	import ua.mihaylenko.canvas.controller.EventManager;
	import ua.mihaylenko.canvas.data.DataManager;
	/**
	 * ...
	 * @author Mihaylenko A.L.
	 */
	public class RectangleCanvas extends Rectangle
	{
		private var _rLeft:Rectangle;
		private var _rTop:Rectangle;
		private var _rRight:Rectangle;
		private var _rBottom:Rectangle;
		private var _rFront:Rectangle;
		
		private static var crMaxFront:Rectangle;
		private static var crMinFront:Rectangle;
		
		private var nMaxWidthCanvas:Number = 64;
		private var nMinWidthCanvas:Number = 10;
		
		private var _nWidthFrontStep:Number;
		private var _nWidthCanvasStep:Number;
		
		public function RectangleCanvas(x:Number = 0.0, y:Number = 0.0, width:Number = 417.0, height:Number = 292.0)
		{
			super(x, y, width, height);
			EventManager.getInstance().addEventListener(CanvasEvent.CHANGE_CANVAS_SIZE, canvasHandler);
			EventManager.getInstance().addEventListener(CanvasEvent.CHANGE_IMAGE_SIZE, canvasHandler);
			crMaxFront = new Rectangle(nMinWidthCanvas, nMinWidthCanvas, Number(this.width -nMinWidthCanvas * 2), Number(this.height - nMinWidthCanvas * 2) );
			crMinFront = new Rectangle(nMaxWidthCanvas, nMaxWidthCanvas, Number(this.width -nMaxWidthCanvas * 2), Number(this.height - nMaxWidthCanvas * 2) );
			
			calculateStepCanvas();
		}
		public function calculateStepCanvas() {
			_nWidthFrontStep = Math.abs(crMinFront.width - crMaxFront.width) / (DataManager.getInatsnce().front.length - 1);
			_nWidthCanvasStep = Math.abs(nMinWidthCanvas - nMaxWidthCanvas) / (DataManager.getInatsnce().canvas.length - 1);
		}
		private function calculateStepCanvasImage(iNum:int) {
			_nWidthFrontStep = Math.abs(crMinFront.width - crMaxFront.width) / (DataManager.getInatsnce().front.length - 1);
			_nWidthCanvasStep = Math.abs(nMinWidthCanvas - nMaxWidthCanvas) / (DataManager.getInatsnce().canvas.length - 1);
		}
		public function  canvasHandler(event:CanvasEvent) {
			var nWidth:Number;
			var nHeight:Number;
			switch(event.type) {
				case CanvasEvent.CHANGE_CANVAS_SIZE:
					nWidth = event.canvasNum * _nWidthCanvasStep + nMinWidthCanvas;
					nHeight = this.height - nWidth * 2;
					break;
				case CanvasEvent.CHANGE_IMAGE_SIZE:
					nWidth = event.pictureNum * _nWidthFrontStep + nMinWidthCanvas;
					nHeight = this.height - nWidth * 2;
					break;
			}
			_rLeft = new Rectangle(0, nWidth, nWidth, nHeight);
			_rRight = new Rectangle(nWidth+nHeight, nWidth, nWidth, nHeight);
			_rTop = new Rectangle(nWidth, 0, nHeight, nWidth );
			_rBottom = new Rectangle(nWidth, this.height - nWidth, nHeight, nWidth);
			_rFront = new Rectangle(_rTop.x, _rLeft.y, nWidth, nHeight);
		}
		public function get _front():Rectangle {
			return _rFront;
		}
		public function get _left():Rectangle {
			return _rLeft;
		}
		public function get _right():Rectangle {
			return _rRight;
		}
		public function get _bottom():Rectangle {
			return _rBottom;
		}
		public function get _top():Rectangle {
			return _rTop;
		}
	}
	
}